package com.example.aaa_viewpager;

import java.util.ArrayList;
import java.util.List;

import android.R.color;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Color;
import android.os.Bundle;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.example.bb_serch.R;


public class MainActivity extends FragmentActivity 
{
	private List<View>viewList;
	//private FragmentTabHost tabHost;
	
	//private PagerTabStrip tab;
	private List<String>titleList;
	private List<Fragment>fragList;
	
	//狀態保存!?
	private final String  SAVE_INSTANCE_TAG = "MainTestActivity.Saveinstancertag";
	
	
	
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //老師說要寫從網站擷取json資料 在這邊寫個讀取行為判定是否更新資料
        
        initView();
        
        	
	}
   
	private void initView() 
	{
		///標題隱藏
	    ActionBar actionBar = getActionBar();  
	    actionBar.hide();  
		
		viewList = new ArrayList<View>();
		FragmentTabHost tabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
		tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
		 //1
	    tabHost.addTab(tabHost.newTabSpec("主頁")
	                          .setIndicator("主頁"), 
	                   fragment1.class, 
	                   null);
	    //2
	    tabHost.addTab(tabHost.newTabSpec("景點排行")
	                          .setIndicator("景點排行"), 
	                          fragment2.class, 
	                   null);
	    //3
	    tabHost.addTab(tabHost.newTabSpec("收藏的民宿")
	                          .setIndicator("收藏的民宿"), 
	                          fragment3.class, 
	                   null);
		/*
		 * 通過View對象去做為ViewPager的數據來源
		 * *///XXX
//		View view1 = View.inflate(this, R.layout.view1, null);
//		View view2 = View.inflate(this, R.layout.view2, null);
//		View view3 = View.inflate(this, R.layout.view3, null);
//		//View view4 = View.inflate(this, R.layout.view4, null);
//		
//		viewList.add(view1);
//		viewList.add(view2);
//		viewList.add(view3);
//		//viewList.add(view4);
//		
//		/*
//		 * 通過Fragment做為ViewPager的數據來源
//		 * */
//		fragList = new ArrayList<Fragment>();
//		fragList.add(new fragment1());
//		fragList.add(new fragment2());
//		fragList.add(new fragment3());
		//fragList.add(new fragment4());
		
		
		//為viewpager頁卡設置標題
//		titleList = new ArrayList<String>();
//		titleList.add("主頁");
//		titleList.add("搜尋");
//		titleList.add("收藏的民宿");
//		titleList.add("關於");
		
		//PagerTabStrip設置一些屬性
//		tab.setBackgroundColor(Color.GRAY);
//		tab.setTextColor(Color.LTGRAY);
//		tab.setDrawFullUnderline(false);
//		tab.setTabIndicatorColor(Color.BLACK);
	
		//創建PagerAdapter的適配器
//		MyPagerAdapter adapter = new MyPagerAdapter(viewList,titleList);XX
//		
//		//ViewPager加載適配器
//	//	pager.setAdapter(adapter);
//		
//		MyFragmentPagerAdapter adapter2 = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragList, titleList);
//		pager.setAdapter(adapter2);
	}
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		// super.onBackPressed();
		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
		dialog.setTitle("離開應用程式...");
		dialog.setMessage("確定要離開?");
		dialog.setPositiveButton("確定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				MainActivity.super.onBackPressed();
			}
		});
		dialog.setNegativeButton("取消",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// Do NOTHING
					}
				});
		dialog.show();
	}
	
	
	
	//如果電話來了，可保存activity狀態!?
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		if(outState != null)
		{
			
			outState.getString(this.SAVE_INSTANCE_TAG,"xyz");
		}
		
		super.onSaveInstanceState(outState);
		
	}

	

   
}
